package org.jeecg.modules.runner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.google.gson.JsonObject;
import org.codehaus.jettison.json.JSONObject;
import org.jeecg.common.util.RedisUtil;

import org.jeecg.modules.entity.TigDevice;
import org.jeecg.modules.service.ITigDeviceService;
import org.jeecg.modules.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ApplicationDeviceRunner implements ApplicationRunner {

    @Autowired
    ITigDeviceService iTigDeviceService;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //Get all devices and make shadows
        QueryWrapper<TigDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ison",1);
        List<TigDevice> devices = iTigDeviceService.list(queryWrapper);
        for(int i=0;i<devices.size();i++){
            try {
                JSONObject jo = ConvertUtil.toJsonTigDevice(devices.get(i));
                redisUtil.set("tig_shadow_" + devices.get(i).getId(), jo);
            }catch (Exception e){
                e.printStackTrace();
            }
        }    }

}
